package com.yijiu.trb;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yijiu.action.BaseAction;
import com.yijiu.network.Request;
import com.yijiu.resource.ActionType;
import com.yijiu.resource.C;
import com.yijiu.response.RequestPersonalInfoResponse;

/* 有标题的activity */
public class BTitleActivity extends BNotitleActivity
{
	protected TextView titleView;

	protected ImageView leftButton;

	protected ImageView rightButton;

	private int sec = 60;

	private RelativeLayout.LayoutParams relativeLp;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		relativeLp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
	}

	/**
	 * 个人信息请求
	 * 
	 * @param appKey
	 */
	private void requestPersonInfo(String appKey)
	{
		String inputStr = "service=personalInfoService";
		encryptBase(inputStr, C.PRIVATE_KEY);
		BaseAction action = new BaseAction(encodedData, encriptData, appKey);
		RequestPersonalInfoResponse requestPersonalInfo = new RequestPersonalInfoResponse();
		Request request = new Request(action, requestPersonalInfo, ActionType.REQUEST_PERSONAL_INFO);
		netManager.excute(request, this, this);
		showProgress("正在获取个人信息");
	}

	/**
	 * 操作成功返回的函数
	 */
	@Override
	public void getResult(Request r)
	{
		super.getResult(r);
		switch (r.getType())
		{
			case ActionType.REQUEST_PERSONAL_INFO:// 请求个人信息
				break;
		}
	}

	/**
	 * 操作失败返回的函数
	 */
	@Override
	public void notifyError(int type, int errorCode, String errorMsg)
	{
		super.notifyError(type, errorCode, errorMsg);
		switch (type)
		{
			case ActionType.REQUEST_PERSONAL_INFO:
				break;
		}
	}

	@Override
	public void setContentView(int layoutResID)
	{
		LinearLayout screenLayout = getScreenView();
		RelativeLayout contentLayout = (RelativeLayout) screenLayout.findViewById(R.id.content_view);
		View contentView = inflater.inflate(layoutResID, null);
		contentLayout.addView(contentView, relativeLp);
		super.setContentView(screenLayout);
	}

	@Override
	public void setContentView(View contentView)
	{
		LinearLayout screenLayout = getScreenView();
		RelativeLayout contentLayout = (RelativeLayout) screenLayout.findViewById(R.id.content_view);
		contentLayout.addView(contentView, relativeLp);
		super.setContentView(screenLayout);
	}

	@Override
	public void setContentView(View contentView, LayoutParams params)
	{
		if (!(params instanceof RelativeLayout.LayoutParams))
		{
			throw new RuntimeException("You must put the RelativeParams");
		}
		LinearLayout screenLayout = getScreenView();
		RelativeLayout contentLayout = (RelativeLayout) screenLayout.findViewById(R.id.content_view);
		contentLayout.addView(contentView, params);
		super.setContentView(screenLayout);
	}

	private LinearLayout getScreenView()
	{
		LinearLayout screenView = (LinearLayout) inflater.inflate(R.layout.screen_layout, null);
		titleView = (TextView) screenView.findViewById(R.id.title_view);
		titleView.getPaint().setFakeBoldText(true);
		leftButton = (ImageView) screenView.findViewById(R.id.left_btn);
		rightButton = (ImageView) screenView.findViewById(R.id.right_btn);
		leftButton.setOnClickListener(titleButtonClickListener);
		rightButton.setOnClickListener(titleButtonClickListener);
		return screenView;
	}

	private OnClickListener titleButtonClickListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			int id = v.getId();
			switch (id)
			{
				case R.id.left_btn:
					leftButtonClick();
					break;
				case R.id.right_btn:
					rightButtonClick();
					break;
			}
		}
	};

	protected void leftButtonClick()
	{
		finish();
	}

	protected void rightButtonClick()
	{
		Intent intent = new Intent(this, MainTabActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
	}

	/**
	 * 60秒倒计时
	 * 
	 * @param btn
	 */
	public void timerCount(final TextView btn)
	{
		btn.setBackgroundResource(R.drawable.btn_login_bg_false);
		btn.setEnabled(false);
		btn.setText("60秒后重新获取");
		final Timer timer = new Timer();
		TimerTask task = new TimerTask()
		{
			@Override
			public void run()
			{
				runOnUiThread(new Runnable()
				{
					@Override
					public void run()
					{
						if (--sec < 0)
						{
							sec = 60;
							btn.setBackgroundResource(R.drawable.login_background);
							btn.setEnabled(true);
							btn.setText("获取验证码");
							timer.cancel();
						}
						else
							btn.setText(sec + "秒后重新获取");
					}
				});
			}
		};
		timer.schedule(task, 1000, 1000);
	}
}
